const {
	Result,
	Logger,
	TimingLogger,
	Clock,
	StatusEnum,
	DictItem
} = require("./class.js");
// 解析数据
function parser(sour = "", reg = /(.*)/, match = "$1", isParse = true, flags = "gi") {
	try {
		if (sour) {
			const strOfSour = sour.toString();
			const regexp = new RegExp(reg, flags);
			const tar = strOfSour.replace(regexp, match)
			return isParse ? JSON.parse(tar.trim()) : tar
		}
	} catch (e) {
		return null;
	}
}

function success(data = "") {
	return new Result({
		data,
		success: true,
		typeName: StatusEnum.SUCCESS_NAME,
		typeCode: StatusEnum.SUCCESS

	});
}

function error(errorMessage = "", exceptionData = "") {
	return new Result({
		exceptionData,
		errorMessage,
		hasErrors: true,
		typeName: StatusEnum.ERROR_NAME,
		typeCode: StatusEnum.ERROR
	});
}

function exception(context = {}, expMsg = "", excepData = "") {
	const {
		ctx: {
			data = "",
			event: {
				action
			} = {},
			context: {
				CLIENTIP,
				CLIENTUA,
				SPACEINFO,
				PLATFORM,
				APPID,
				DEVICEID
			} = {}
		} = {}
	} = context || {};

	uniCloud.logger.error(new Logger({
		action,
		message: expMsg,
		exceptionData: excepData,
		inputData:data,
		date: Clock.now(),
		info: {
			CLIENTIP,
			CLIENTUA,
			SPACEINFO,
			PLATFORM,
			APPID,
			DEVICEID
		}
	}))
	return new Result({
		hasErrors: true,
		exceptionData: excepData + "",
		errorMessage: expMsg,
		inputData:data,
		typeName: StatusEnum.EXCEPTION_NAME,
		typeCode: StatusEnum.EXCEPTION

	})
}

// 定时任务 
function timingLogger({
	occur_dt,
	typeCode,
	typeName,
	info,
	action,
	exceptionData
} = {}) {
	return new TimingLogger({
		occur_dt: Clock.now(),
		occur_date: Clock.now("YYYY-MM-DD"),
		occur_year_month: Clock.now("YYYY-MM"),
		occur_year: Clock.now("YYYY"),
		typeCode: StatusEnum[typeName] || "",
		typeName,
		info,
		action,
		exceptionData
	});

}

// 判断类型 
function typeOf(sour, tarType) {
	const args = Array.from(arguments);
	const sourInstType = Object.prototype.toString.call(sour);
	const map = {
		'[object Boolean]': StatusEnum.BOOLEAN,
		'[object Number]': StatusEnum.NUMBER,
		'[object String]': StatusEnum.STRING,
		'[object Function]': StatusEnum.FUNCTION,
		'[object Array]': StatusEnum.ARRAY,
		'[object Date]': StatusEnum.DATE,
		'[object RegExp]': StatusEnum.REGEXP,
		'[object Undefined]': StatusEnum.UNDEFINED,
		'[object Null]': StatusEnum.NULL,
		'[object Object]': StatusEnum.OBJECT
	};
	const mapType = map[sourInstType];
	return args.length > 1 ? tarType.split(",").includes(mapType) : mapType
}


function isBoolean(preProp) {
	return typeOf(preProp, StatusEnum.BOOLEAN);
}

function isNumber(preProp) {
	return typeOf(preProp, StatusEnum.NUMBER);
}

function isString(preProp) {
	return typeOf(preProp, StatusEnum.STRING);
}

function isFunction(preProp) {
	return typeOf(preProp, StatusEnum.FUNCTION);
}

function isArray(preProp) {
	return typeOf(preProp, StatusEnum.ARRAY);
}

function isDate(preProp) {
	return typeOf(preProp, StatusEnum.DATE);
}

function isRegExp(preProp) {
	return typeOf(preProp, StatusEnum.REGEXP);
}

function isUndefined(preProp) {
	return typeOf(preProp, StatusEnum.UNDEFINED);
}

function isNull(preProp) {
	return typeOf(preProp, StatusEnum.NULL);
}

function isObject(preProp) {
	return typeOf(preProp, StatusEnum.OBJECT);
}

function processCallFunResult ({result = {}, success:reqSuccess} = {},message = ""){
	
	if(!reqSuccess){
		return error(`${message}请求异常;`,{result});
	}
	return result;
}

module.exports = {
	parser,
	Logger,
	typeOf,
	success,
	error,
	exception,
	Clock,
	timingLogger,
	StatusEnum,
	Result,
	DictItem,
	isBoolean,
	isNumber,
	isString,
	isArray,
	isFunction,
	isDate,
	isRegExp,
	isUndefined,
	isNull,
	isObject,
	processCallFunResult
	
}
